package cn.com.server.controller;


import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import com.alibaba.fastjson.JSON;

import cn.com.server.common.AjaxCodeUtil;
import cn.com.server.common.AjaxObject;
import cn.com.server.shiro.entity.User;
import cn.com.server.extend.view.TestBean;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 功能列表 前端控制器
 * </p>
 *
 * @author qing
 * @since 2017-11-01
 */
@Controller
@RequestMapping("")
public class IndexController {
	
	private Logger logger = Logger.getLogger(IndexController.class);
	
	@RequestMapping("/getSystemInfo")
	@ResponseBody
	public AjaxObject getSystemInfo(HttpServletRequest request, HttpServletResponse response) {
		try {
			Subject subject = SecurityUtils.getSubject();
			User user = (User) subject.getPrincipal();
			if(user!=null) {
				AjaxObject ajaxObject = AjaxCodeUtil.getAjaxObject("success");
	            ajaxObject.setData(user);
	            return ajaxObject;
			}else {
				return AjaxCodeUtil.getAjaxObject("loginException");
			}
            
        } catch(Exception e) {
            e.printStackTrace();
            logger.info("登录失败");
            AjaxObject ajaxObject = AjaxCodeUtil.getAjaxObject("loginException");
            return ajaxObject;
        }
	}
	
	@RequestMapping("/changeLanguage")
	@ResponseBody
	public AjaxObject changeLanguage(String language,HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		// 当language为null时,则以客户浏览器语进行言国际化  
        if(language != null ) {  
            Locale locale = null;  
            if (language.equals("zh_CN")) {  
                // 汉语-中国  
                locale = new Locale("zh", "CN");  
            } else if (language.equals("en_US")) {  
                // 英语-美国  
                locale = new Locale("en", "US");  
            } else if (language.equals("ja_JP")) {  
                // 日语-日本  
                locale = new Locale("ja", "JP");  
            } else {  
                locale = new Locale("zh", "CN");  
            }  
            //以上内容是根据配置文件的配置信息实例化Locale对象。  
            request.getSession(true).setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale);  
        }
		return AjaxCodeUtil.getAjaxObject("success");  
	}
	
	@RequestMapping("/loginUser")
	@ResponseBody
	public AjaxObject loginUser(String userName,String password,HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(userName,password);
		Subject subject = SecurityUtils.getSubject();
		try {
            subject.login(usernamePasswordToken);   //完成登录
            User user = (User) subject.getPrincipal();
            request.getSession(true).setAttribute("user", user);
            logger.info(user.getName()+"登录成功");
            AjaxObject ajaxObject = AjaxCodeUtil.getAjaxObject("success");
            ajaxObject.setData(user);
            return ajaxObject;
        } catch(Exception e) {
            e.printStackTrace();
            logger.info("登录失败");
            AjaxObject ajaxObject = AjaxCodeUtil.getAjaxObject("loginException");
            return ajaxObject;
        }
	}
	
	@RequestMapping("/logOut")
	@ResponseBody
	public AjaxObject logOut(HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		try {
			 Subject subject = SecurityUtils.getSubject();
			 request.getSession(true).removeAttribute("user");
		     subject.logout();
		     logger.info("退出成功");
		     return AjaxCodeUtil.getAjaxObject("success");
		} catch (Exception e) {
			logger.info("退出异常");
            return AjaxCodeUtil.getAjaxObject("fail");
		}
	}
	
	@RequestMapping("/testBean")
	@ResponseBody
	public void testBean(@RequestBody TestBean testBean) {
//		<script type="text/javascript">
//		$.ajax({
//			type : "POST",
//			url : "/testBean",
//			data: JSON.stringify({name:1212,ids:[1,2,3]}),
//			contentType: 'application/json;charset=utf-8',//指定请求的数据格式为json,这样后台才能用
//			dataType : "json",
//			success : function(res) {
//				if(res.code==0){
//					location.href = requestContextPath+"/login";
//				}else{
//					layer.msg(res.msg);
//				}
//			}
//		});
//		</script>
		System.out.println(JSON.toJSONString(testBean));
	}
}
